package org.focusj.scala.other

object ExtractorTest extends App {
    object Strings {
        def unapply(v: Object): Option[String] =
            v match {
                case v: String => Some(v)
                case _ => Some(v.toString)
            }
        def unapplySeq(n: Int) =
            n match {
                case 1 => Some(Seq(1))
                case 2 => Some(Seq(1, 2))
            }
    }

    def toInt(s: String): Option[Int] =
        s match {
            case "5" => Some(5)
            case _ => None
        }

    /*def main(args: Array[String]) {
        val (r, rr) = BigInt(10) /% 3
        println(r)
        println(rr)

        "1" match {
            case Strings(n) => println(n)
        }

        val a = toInt("1")
        print(a.getOrElse(0))
    }*/

    case object +: {
        def unapply[T](input: List[T]) =
            if (input.isEmpty) None else Some((input.head, input.tail))
    }
    1 +: 7 +: 8 +: 9 +: Nil match {
        case first +: second +: rest => println(first + second + rest.length)
    }

    //模拟枚举
    sealed abstract class TrafficLight
    case object Red extends TrafficLight
    case object Yellow extends TrafficLight
    case object Green extends TrafficLight

    val color: TrafficLight = Red
    color match {
        case Red => print("red")
        case Green => print("green")
        case Yellow => print("yellow")
    }
}